#include <iostream>
#include <cstdio>
#include "dsmi_common_interface.h"

#define CHECK_RET(ret, funname) {if (ret != 0){std::cout << "call " << funname << "failed!" << std::endl;return -1;}}

int main()
{
//    freopen("/dev/null", "w", stdout);
//    freopen("/dev/null", "w", stderr);
//    fclose(stdout);
    const char *placeholder = "--------------------------------------------------------";

    int device_count = 0;
    const char *health_state[] = {"OK", "一般告警", "重要告警", "紧急告警"};
    unsigned int phealth, mem_rate, aicore_rate;
    int temperature;
    struct dsmi_power_info_stru power_info{};
    struct dsmi_memory_info_stru pdevice_memory_info{};
    struct dsmi_hbm_info_stru pdevice_hbm_info{};

    CHECK_RET(dsmi_get_device_count(&device_count), "dsmi_get_device_count");
    printf("%s\n", placeholder);
    printf("NPU  Health  AICore(%%)  HBM-Usage(MB)  Power(W)  Temp(C)\n");
    for (int device_id = 0; device_id < device_count; ++device_id)
    {
        dsmi_get_device_health(device_id, &phealth);
        dsmi_get_device_power_info(device_id, &power_info);
        dsmi_get_device_utilization_rate(device_id, UTLRATE_TYPE_AICORE, &aicore_rate);
        dsmi_get_device_temperature(device_id, &temperature);
        dsmi_get_memory_info(device_id, &pdevice_memory_info);
        dsmi_get_hbm_info(device_id, &pdevice_hbm_info);

        printf("%s\n", placeholder);
        printf(" %-4d", device_id);
        if (0 <= phealth && phealth <= 3)
            printf("  %-6s", health_state[phealth]);
        else
            printf(" bad\t");
        printf("   %-7d", aicore_rate);
        printf("%5lu / %5lu    ", pdevice_hbm_info.memory_usage / 1024, pdevice_hbm_info.memory_size / 1024);
        printf("%5.1f      ", power_info.power / 10.);
        printf("%3d\n", temperature);
    }
    printf("%s\n", placeholder);

    return 0;
}